//
//  ViewController.m
//  2048-for-ios
//
//  Created by User-08 on 4/7/14.
//  Copyright (c) 2014 IOTrainingCamp. All rights reserved.
//

#import "ViewController.h"
#import "NumberView.h"
#import "config.h"
#import "GameViewController.h"
#import "IOTCGame.h"


@interface ViewController ()

@property (strong, nonatomic) IBOutlet UISwipeGestureRecognizer *swipeRightRecognizer;
@property (strong, nonatomic) IBOutlet UISwipeGestureRecognizer *swipeLeftRecognizer;
@property (strong, nonatomic) IBOutlet UISwipeGestureRecognizer *swipeDownRecognizer;
@property (strong, nonatomic) IBOutlet UISwipeGestureRecognizer *swipeUPRecognizer;

@property (weak, nonatomic) IBOutlet UIView *gameContainer;
@property GameViewController* gameController;
@property (nonatomic) IBOutlet UIView* scoresAndButtons;

//TODO: scores should also become a container
@property (weak, nonatomic) IBOutlet UILabel *gameLabel;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (weak, nonatomic) IBOutlet UIButton *restartGameButton;
@property id scoreObserver;

@property (strong)IOTCGame* game;

@end

@implementation ViewController

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        self.scoreObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"NewScore" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
            if ([note.object isKindOfClass:[NSNumber class]]) {
                self.scoreLabel.text = [NSString stringWithFormat:@"Score: %@", (NSNumber*)note.object];
            }
        }];
    }
    return self;
}
-(void)dealloc{

  [[NSNotificationCenter defaultCenter] removeObserver:self.scoreObserver];
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.game = [IOTCGame sharedInstance];
    self.scoresAndButtons.backgroundColor = [UIColor colorWithRed:0.98 green:0.973 blue:0.937 alpha:1.0];
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.973 blue:0.937 alpha:1.0];
    self.gameLabel.font =  [UIFont systemFontOfSize:kNumberFontSize];
    self.gameLabel.textColor = [UIColor colorWithRed:0.467 green:0.431 blue:0.396 alpha:1.0];
    self.scoreLabel.font = [UIFont systemFontOfSize:kLabelFontSize];
    self.restartGameButton.titleLabel.font = [UIFont systemFontOfSize:kLabelFontSize];
    
    self.swipeDownRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
    self.swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    self.swipeUPRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)swipeGesture:(id)sender {
    if([sender isKindOfClass:[UIGestureRecognizer class]])
    {
        //fixing animations lag
        [self.game updateBoard];
        
        switch ([sender direction]) {
            case UISwipeGestureRecognizerDirectionDown:
                [self.game moveInDirection:DOWN];
                break;
            case UISwipeGestureRecognizerDirectionLeft:
                [self.game moveInDirection:LEFT];
                break;
            case UISwipeGestureRecognizerDirectionRight:
                [self.game moveInDirection:RIGHT];
                break;
            case UISwipeGestureRecognizerDirectionUp:
                [self.game moveInDirection:UP];
                break;
            default:
                break;
        }
    }

}

- (IBAction)startANewGame:(id)sender {
    [self.game loadANewGameWithSize:self.game.size];
}

-(void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    [self.scoresAndButtons removeConstraints:[self.scoresAndButtons constraints]];
    
    [self.scoresAndButtons setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.gameContainer setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.gameLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.scoreLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.restartGameButton setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view removeConstraints:self.view.constraints];
    
     NSDictionary* viewsNames = @{@"scoreView" : self.scoresAndButtons, @"gameView" : self.gameContainer};
    
    //game and scores relative positioning
    [self.view addConstraint:[NSLayoutConstraint  constraintWithItem:self.gameContainer attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.gameContainer attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0 ]];
    [self.view addConstraint:[NSLayoutConstraint  constraintWithItem:self.scoresAndButtons attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.gameContainer attribute:NSLayoutAttributeHeight multiplier:0 constant:100]];
    

    
    if(UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation]) )
    {
        NSLog(@"landscape");
        //game and score views relative oriantation
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-25-[scoreView]-1-|" options:0 metrics:nil views:viewsNames]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-1-[scoreView]-[gameView]-1-|" options:0 metrics:nil views:viewsNames]];
        [self.view addConstraint:[NSLayoutConstraint  constraintWithItem:self.gameContainer attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.scoresAndButtons attribute:NSLayoutAttributeTop multiplier:1.0 constant:0 ]];
        [self.view addConstraint:[NSLayoutConstraint  constraintWithItem:self.gameContainer attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.scoresAndButtons attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0 ]];
        
        //scores constraints
        [self.scoresAndButtons addConstraint: [NSLayoutConstraint constraintWithItem:self.gameLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.scoresAndButtons attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
        [self.scoresAndButtons addConstraint:[NSLayoutConstraint  constraintWithItem:self.gameLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.scoresAndButtons attribute:NSLayoutAttributeCenterY multiplier:0.5 constant:-20 ]];
        [self.scoresAndButtons addConstraint:[NSLayoutConstraint  constraintWithItem:self.scoreLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.scoresAndButtons attribute:NSLayoutAttributeCenterX multiplier:1 constant:0 ]];
        [self.scoresAndButtons addConstraint:[NSLayoutConstraint  constraintWithItem:self.scoreLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.scoresAndButtons attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0 ]];
        [self.scoresAndButtons addConstraint:[NSLayoutConstraint  constraintWithItem:self.restartGameButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.scoresAndButtons attribute:NSLayoutAttributeCenterY multiplier:1.5 constant:0 ]];
        [self.scoresAndButtons addConstraint:[NSLayoutConstraint  constraintWithItem:self.restartGameButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.scoresAndButtons attribute:NSLayoutAttributeCenterX multiplier:1 constant:0 ]];

    }
    else{
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-25-[scoreView]-1-[gameView]-1-|" options:0 metrics:nil views:viewsNames]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-1-[scoreView]-1-|" options:0 metrics:nil views:viewsNames]];
        [self.view addConstraint:[NSLayoutConstraint  constraintWithItem:self.gameContainer attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.scoresAndButtons attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0 ]];
        [self.view addConstraint:[NSLayoutConstraint  constraintWithItem:self.gameContainer attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.scoresAndButtons attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0 ]];
        
        //scores constraints
        [self.scoresAndButtons addConstraint: [NSLayoutConstraint constraintWithItem:self.gameLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.scoresAndButtons attribute:NSLayoutAttributeCenterX multiplier:0.4 constant:0]];
        [self.scoresAndButtons addConstraint:[NSLayoutConstraint  constraintWithItem:self.gameLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.scoresAndButtons attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0 ]];
        [self.scoresAndButtons addConstraint:[NSLayoutConstraint  constraintWithItem:self.scoreLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.scoresAndButtons attribute:NSLayoutAttributeCenterY multiplier:0.5 constant:0 ]];
        [self.scoresAndButtons addConstraint:[NSLayoutConstraint  constraintWithItem:self.scoreLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.scoresAndButtons attribute:NSLayoutAttributeCenterX multiplier:1.5 constant:0 ]];
        [self.scoresAndButtons addConstraint:[NSLayoutConstraint  constraintWithItem:self.restartGameButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.scoresAndButtons attribute:NSLayoutAttributeCenterY multiplier:1.5 constant:0 ]];
        [self.scoresAndButtons addConstraint:[NSLayoutConstraint  constraintWithItem:self.scoreLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.restartGameButton attribute:NSLayoutAttributeLeading multiplier:1 constant:0 ]];
        
    }
}



@end
